//
//  ViewController.swift
//  tdl
//
//  Created by Qingke on 16/4/11.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class CheckListViewController: UITableViewController,AddTaskDelegate {
    
    
 
    
    var tasks = [ToDo]()

    override func viewDidLoad() {
        
        let task1 = ToDo()
        task1.name = "学习"
        tasks.append(task1)
        
        let task2 = ToDo()
        task2.name = "看电影"
        tasks.append(task2)
        
        let task3 = ToDo()
        task3.name = "面试"
        tasks.append(task3)
        
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    

    //保存
    func addTaskViewController(viewController: AddTaskViewController, task: ToDo) {
        tasks.append(task)
        
        let indexPath = NSIndexPath(forRow: tasks.count - 1, inSection: 0)
        tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
        //reloaddate（），在界面上显示

    }
    
    func editTaskViewController(viewController: AddTaskViewController, task: ToDo) {
        let index = tasks.indexOf(task)
        
        if let index = index {
            let indexPath = NSIndexPath(forRow: index, inSection: 0)
            let cell = tableView.cellForRowAtIndexPath(indexPath) as! myView
            configureCell(cell, task: task)
            
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tasks.count
    }
    
    //创建cell
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! myView
                             //可以重用cell
        
        let task = tasks[indexPath.row]
        configureCell(cell, task: task)
        return cell
    }
    
    func configureCell(cell:myView,task:ToDo) {
        cell.customLabel.text = task.name
        
        let checkMarkLabel = cell.viewWithTag(1001) as! UILabel
        if task.isCheckd {
            checkMarkLabel.text = "√"
        }else {
            checkMarkLabel.text = ""
        }

    }
    
    //点击操作
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        let task = tasks[indexPath.row]
        if let cell = tableView.cellForRowAtIndexPath(indexPath) {//cell可能为空
            let checkMarkLabel = cell.viewWithTag(1001) as! UILabel
            if checkMarkLabel.text == "√" {
                checkMarkLabel.text = ""
                task.isCheckd = true
            }else {
                checkMarkLabel.text = "√"
                task.isCheckd = false

            }
        }
    
        
    }
    
    //添加和编辑
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        
        let navigationVC = segue.destinationViewController as!UINavigationController
        let addTaskVC = navigationVC.topViewController as! AddTaskViewController

        if segue.identifier == "addTask" {
            addTaskVC.addTaskDelegate = self
        } else if segue.identifier == "editTask" {
            
            
            let cell = sender as! UITableViewCell
            let indexPath = tableView.indexPathForCell(cell)
            
            if let indexPath = indexPath {
                let task = tasks[indexPath.row]
                addTaskVC.editToTask = task
                addTaskVC.addTaskDelegate = self

            }
           
        }
        
    }

    
    //删除操作commiteditingstyle,caneditrowatindexpath是判断是否可以进行删除操作
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        tasks.removeAtIndex(indexPath.row) //从数组里删除
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) //从界面删除
    }


}

